######################################################
# Code to create Figure OB2: Over Time Trends in Extremity Perceptions
# By Target Party and Respondent partisanship
######################################################

############################
#Packages
############################
library(survey)
library(srvyr)
library(directlabels)
library(rio)
library(directlabels)
library(tidyverse)

############################
#Import Data and Clean
############################

#######Data
combdata <- import("./Data/pew_cnn_comb.Rda")
#combdata$sdate <- as.Date(combdata$sdate)       

#######Declare Survey Design
comb_design <- combdata %>%
  filter(!is.na(weight)) %>%  #This removes some observations with missing weight data
  srvyr::as_survey_design(ids = 1, weights=weight)

########Get weighted means
rep_means <- comb_design %>%
  mutate(rep_extreme = factor(rep_extreme, 
                              levels=c(0,1), 
                              labels=c("Not Too Extreme", "Too Extreme")), 
         pid2 = car::recode(pid_3, '3=NA'), 
         pid2 = factor(pid2, levels=c(1,2), 
                       labels=c("Democrat", "Republican"))) %>%
  filter(!is.na(rep_extreme) & !is.na(pid2)) %>%
  group_by(sdate, pid2, rep_extreme) %>%
  summarize(proportion = srvyr::survey_mean(na.rm=T)) %>%
  mutate(lower = proportion - (1.96*proportion_se), 
         upper = proportion + (1.96*proportion_se), 
         target = "Republican Party") 


dem_means <- comb_design %>%
  mutate(dem_extreme = factor(dem_extreme, 
                              levels=c(0,1), 
                              labels=c("Not Too Extreme", "Too Extreme")), 
         pid2 = car::recode(pid_3, '3=NA'), 
         pid2 = factor(pid2, levels=c(1,2), 
                       labels=c("Democrat", "Republican"))) %>%
  filter(!is.na(dem_extreme) & !is.na(pid2)) %>%
  group_by(sdate, pid2, dem_extreme) %>%
  summarize(proportion = srvyr::survey_mean(na.rm=T)) %>%
  mutate(lower = proportion - (1.96*proportion_se), 
         upper = proportion + (1.96*proportion_se), 
         target = "Democratic Party") 

comb_means <- bind_rows(rep_means, dem_means) %>%
  mutate(response = case_when(
    !is.na(rep_extreme) ~ rep_extreme, 
    is.na(rep_extreme) ~ dem_extreme)) %>%
  select(-rep_extreme, -dem_extreme) %>%
  mutate(sdate = as.Date(sdate))


#######Export Overall Means
export(comb_means, "./Data/party_means_bypid.csv")

#######Figure

c1 <- comb_means %>% filter(response == "Too Extreme")

ends <- c1 %>%
  group_by(pid2, target) %>%
  top_n(1, sdate) %>%
  pull(proportion)

diff <- c1 %>%
  select(-c(proportion_se, lower, upper)) %>%
  pivot_wider(names_from="target", 
              values_from="proportion") %>%
  mutate(diff = case_when(
    pid2 == "Democrat" ~ `Republican Party` - `Democratic Party`, 
    pid2 == "Republican" ~ `Democratic Party` - `Republican Party`)) %>%
  ggplot(aes(x=sdate, y=diff, shape=pid2, linetype=pid2)) + 
  geom_point() + 
  geom_line() + 
  scale_y_continuous(limits=c(0,1)) + 
  theme_bw(14) + 
  labs(title="Partisan Gap (Proportion Out - Proportion In)", 
       y = "Partisan Gap", 
       x = "Year of Survey", 
       shape="Party ID", 
       linetype="Party ID") + 
  scale_x_date(date_breaks= "4 year", date_labels="%Y") + 
  theme(legend.position=c(0.30,0.95),
        legend.direction="vertical", 
        legend.background = element_rect(), 
        legend.box.background = element_rect(colour = "black"))

diff1 <- lemon::reposition_legend(diff, 'top left', offset=0.002)


trends <- ggplot(c1, aes(x=sdate, y=proportion, shape=target, linetype=target)) + 
  geom_point() + 
  geom_line() + 
  facet_wrap(. ~ pid2) + 
  scale_y_continuous(limits=c(0,1)) + 
  theme_bw(14) + 
  labs(title="Party Too Extreme?", 
       y = "% Saying Too Extreme", 
       x = "Year of Survey", 
       shape="Party", 
       linetype="Party") + 
  scale_x_date(date_breaks= "4 year", date_labels="%Y") + 
  theme(legend.position=c(0.30,0.95),
        legend.direction="vertical", 
        legend.background = element_rect(), 
        legend.box.background = element_rect(colour = "black")) 

trends_fig1 <- lemon::reposition_legend(trends, 'top left', 
                                        offset=0.002, 
                                        panel = 'panel-1-1')


ggpubr::ggarrange(trends_fig1, diff1, 
                  labels=c("A", "B"))

ggsave("figure_ob2.png", 
       height=6, width=14, dpi=1200)




c1 %>%
  select(-c(proportion_se, lower, upper)) %>%
  pivot_wider(names_from="target", 
              values_from="proportion") %>%
  mutate(diff = case_when(
    pid2 == "Democrat" ~ `Republican Party` - `Democratic Party`, 
    pid2 == "Republican" ~ `Democratic Party` - `Republican Party`)) %>%
  group_by(pid2) %>%
  summarize(diff = mean(diff))
